package concurrent;

import java.util.concurrent.TimeUnit;

/**
 * 
 * 在守护线程中默认创建的也是守护线程,但是也可以创建非守护线程
 * 
 * @author jyzjyz12@163.com
 * @since 2017年2月23日 上午9:23:10
 */
public class DaemonsTest1 {

    /**
     * 
     * @param args
     * 
     * @throws InterruptedException
     */

    public static void main(String[] args) throws InterruptedException {
        Thread d = new Thread(new Daemon());
        d.setDaemon(true); // 必须在启动线程前调用
        d.start();
        System.out.println("d.isDaemon() = " + d.isDaemon() + ".");
        TimeUnit.SECONDS.sleep(1);
    }
}

class DaemonSpawn implements Runnable {
    public void run() {
        while (true) {
            Thread.yield();
        }
    }
}

class Daemon implements Runnable {
    private Thread[] t = new Thread[10];

    public void run() {
        for (int i = 0; i < t.length; i++) {
            t[i] = new Thread(new DaemonSpawn());
            t[i].setDaemon(false);
            t[i].start();
            System.out.println("DaemonSpawn " + i + " started.");
        }

        for (int i = 0; i < t.length; i++) {
            System.out.println("t[" + i + "].isDaemon() = " + t[i].isDaemon() + ".");
        }

        while (true) {
            Thread.yield();
        }
    }
}